﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;

namespace 实验六GDI_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int b = 0;
        private void 点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 1;
        }

        private void 点ToolStripMenuItem_Paint(object sender, PaintEventArgs e)
        {
     
        }

        private void 线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 2;
        }

        private void 多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 3;
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if(b==1)
            {
                Graphics g = e.Graphics;
                SolidBrush sb = new SolidBrush(Color.Black);
                foreach(Point pt in lp)
                {
                    g.FillEllipse(sb, pt.X, pt.Y, 4, 4);
                    
                }
                sb.Dispose();
                g.Dispose();
                

            }
            if (b == 2)
            {
                Graphics g = e.Graphics;
                Pen pen = new Pen(Color.Red, 2);
                foreach (Myline templine in ml)
                {
                    g.DrawLine(pen, templine.start.X, templine.start.Y, templine.end.X, templine.end.Y);
                }
                pen.Dispose();
                g.Dispose();
            }
            if (b == 3)
            {
                Graphics g = e.Graphics;
                Pen pen = new Pen(Color.Red, 2);

                foreach (DBX tempbx in dbx1)
                {
                    g.DrawPolygon(pen, tempbx.dbx.ToArray());
                }
                pen.Dispose();
                g.Dispose();
                
            }
            if (b == 4)
            {
                Graphics g = e.Graphics;
                Pen pen =new Pen(Color.Red,2);
                foreach(QX tempqx in qx1)
                {
                    g.DrawCurve(pen, tempqx.qx.ToArray());
                }
                pen.Dispose();
                g.Dispose();
            }
 
        }
        List<Point> lp =new List<Point>();   //创建点集合
        List<Myline> ml = new List<Myline>();   //创建线集合
        List<DBX> dbx1= new List<DBX>();      //创建多边形集合
        List<QX> qx1 = new List<QX>();     //创建曲线集合
        QX tempqx = new QX();
        //DBX tempdbx;
        DBX tempdbx = new DBX();
        Myline templine = new Myline();
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (b == 1)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                lp.Add(pt);     //收集点数据加入到点集合
                Invalidate();   //无效化 使画布重绘
            }
            if(b==2)
            {
                templine = new Myline();
                templine.start.X = e.X;   
                templine.start.Y = e.Y;
                //Invalidate();
                ml.Add(templine);   //画线 收集起点数据

            }

            if (b == 3 & e.Button == MouseButtons.Left)
            {
                Point pt = new Point();
                //Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                //    tempdbx = new DBX();
                tempdbx.dbx.Add(pt);     //将点数据收集到创建的多边形类中的点集合中
            }

            if (b==3&&e.Button == MouseButtons.Right)
            {
                dbx1.Add(tempdbx);    //将创建的对象加入到多边形的集合中
                tempdbx = new DBX();
                Invalidate();
            }
            if (b == 4 & e.Button == MouseButtons.Left)
            {
                Point pt = new Point();
                pt.X = e.X;
                pt.Y = e.Y;
                tempqx.qx.Add(pt);

            }
            if (b == 4 && e.Button == MouseButtons.Right)
            {
                qx1.Add(tempqx);
                tempqx = new QX();
                Invalidate();
                //if (qx1.Count < 4)
                //{
                //    MessageBox.Show("点数收集不足");
                //    qx1.Clear();
                //}
                //qx1.Clear();
            }

            
        }
        //创建类 线数据
        class Myline  
        {
            public Point start;
            public Point end;
            public Myline()
            {
                start = new Point();
                end = new Point();
            }
            
            
        }
        //创建类 多边形数据
        class DBX
        {
           
            public List<Point> dbx;
            public DBX()
            {
                dbx = new List<Point>();
            }
        }
        //创建类 曲线数据
        class QX
        {
            public List<Point>qx;
            public QX()
            {
                qx = new List<Point>();
            }
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (b == 2)
            {
                // templine = new Myline();
                templine.end.X = e.X;
                templine.end.Y = e.Y;
                ml.Add(templine);
                Invalidate();
            }


            /* if (b==3&e.Button == MouseButtons.Left)
             {
                 Point pt = new Point();
                 //Point pt = new Point();
                 pt.X = e.X;
                 pt.Y = e.Y;
             //    tempdbx = new DBX();
                 tempdbx.dbx.Add(pt);
             }
          /*   if (e.Button == MouseButtons.Right)
             {
                 dbx1.Add(tempdbx);
                 tempdbx = new DBX();
                 Invalidate();
             }*/
            
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            /* if (b == 3 & e.Button == MouseButtons.Left)
             {
                 Point pt = new Point();
                 //Point pt = new Point();
                 pt.X = e.X;
                 pt.Y = e.Y;
                 //    tempdbx = new DBX();
                 tempdbx.dbx.Add(pt);
             }*/
        }

        private void 曲线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 4;
        }

        private void 纯色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();   //创建颜色对话框对象
            cd.ShowDialog();       //打开颜色对话框对象
            Graphics g = this.CreateGraphics();
            SolidBrush sb = new SolidBrush(cd.Color);
            g.FillRectangle(sb, this.ClientRectangle);
        }

        private void 渐变色ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd = new ColorDialog();
            cd.ShowDialog();
            ColorDialog cd1 = new ColorDialog();
            cd1.ShowDialog();
            Graphics g = this.CreateGraphics();
            LinearGradientBrush lgb = new LinearGradientBrush(this.ClientRectangle, cd.Color, cd1.Color, LinearGradientMode.Horizontal);
            g.FillRectangle(lgb, this.ClientRectangle);
        }

        private void 图案ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog cd1 = new ColorDialog();
            cd1.ShowDialog();
            ColorDialog cd2=new ColorDialog();
            cd2.ShowDialog();
            Graphics g = this.CreateGraphics();
            HatchBrush hb = new HatchBrush(HatchStyle.Vertical, cd1.Color, cd2.Color);    //HatchBrush画刷
            g.FillRectangle(hb, this.ClientRectangle);
            
        }

        private void 图像填充ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();    //创建颜色对话框对象
            ofd.ShowDialog();      //打开颜色对话框
            string[]st = ofd.FileNames;    //获取对话框中选定文件的文件名，此处文件名以数组的方式存在，故定义st数组
            string st1=null;
            for(int i=0;i<st.Length;i++)    //将数组中的元素转成字符串
            {
                st1=st1+st[i];
            }
            this.BackgroundImage = Image.FromFile(st1);    //将背景设置成选定的文件，st1为文件名
            this.BackgroundImageLayout = ImageLayout.Stretch;  //调整背景大小
        }
        string path = "D:\\1\\1.txt";
        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (File.Exists(path))     //存在该文件时删除之
                File.Delete(path);
            else
            {
                FileStream fs = File.OpenWrite(path);      //建立文件写文件流
                StreamWriter sw = new StreamWriter(fs);    //建立文件流写对象
                if(b==1)
                {
                    foreach (Point pt in lp)
                    {
                        sw.Write(pt);
                        sw.Write(',');
                    }
                }
                if (b == 2)
                {
                    
                }
                sw.Close();
                fs.Close();
                MessageBox.Show("保存成功");
                
            }
        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FileStream fs = File.OpenRead(path);          //建立文件读文件流
            StreamReader sr = new StreamReader(fs);       //建立文件流读对象
            string a=null;
            a = sr.ReadLine();   //将点的坐标写成字符串
            string[] srt;
            string a1 = null;
            sr.Close();
            fs.Close();
            //将a1写出纯数字与，的字符串
            foreach (char c in a)
            {
                if ((Convert.ToInt32(c) >= 48 && Convert.ToInt32(c) <= 57)||Convert.ToInt32(c)==44)
                {
                    a1 = a1 + c; 
                }
            }
            srt = a1.Split(',');   //将坐标写出数组形式
            Graphics g = this.CreateGraphics();
            SolidBrush sb = new SolidBrush(Color.Black);
            //在画布上画出点
            for (int i = 1; i <= srt.Length - 1; i = i + 2)
            {
                g.FillEllipse(sb, Convert.ToInt32(srt[i-1]), Convert.ToInt32(srt[i]), 4, 4);
            }
            //sr.Close();
            //fs.Close();
            
            
            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
